package com.cxs.controller;

import com.cxs.dto.CommentDTO;
import com.cxs.model.NewsComment;
import com.cxs.service.NewsCommentService;
import cxs.utils.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/*
 * @Author:cxs
 * @Date:2021/10/10  13:40
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private NewsCommentService newsCommentService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(Long id){
        if(id == null) Result.failare("id不能为空");
        return Result.OK("ok",newsCommentService.list(id));
    }

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public Result insert(@RequestBody @Valid CommentDTO comment, BindingResult result){
        if(result.hasErrors()){
            return Result.failare(result.getFieldError().getDefaultMessage());
        }
        NewsComment newsComment = new NewsComment();
        BeanUtils.copyProperties(comment,newsComment);
        System.out.println(newsComment);
        return newsCommentService.add(newsComment);
    }
}
